import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class CSVImporter {
	
	private String divider = ",";
	private static CSVImporter instance = null;
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
	
	private CSVImporter(){}
	
	public static CSVImporter getInstance(){
		if(CSVImporter.instance == null){
			CSVImporter.instance = new CSVImporter();
		}
		
		return CSVImporter.instance;
	}
	
	public void importTransactionData(String customerID) {
		 
		String csvFile = "datasets/Hilti_dataset_training.csv";
		BufferedReader br = null;
		String line = "";
	 
		try {
			br = new BufferedReader(new FileReader(csvFile));
			System.out.println("---STARTING IMPORT OF customerID: "+customerID+" ---");
			while ((line = br.readLine()) != null) {
				
				//remove " " in Strings
				line=line.replace("\"", "");
			        // use comma as separator
				String[] data = line.split(divider);
				
				if(data[1].equals(customerID)){
					boolean holUser;
					if(data[14].equals("Yes")){
						holUser = true;
					}else{
						holUser = false;
					}
					try {
						new DataTransaction(Integer.valueOf(data[1]), data[2], data[3], data[4], Integer.valueOf(data[5]), sdf.parse(data[6]), Integer.valueOf(data[7]), data[8],Float.valueOf(data[9]), data[10], data[11], data[12], data[13], holUser, data[15], data[16], Integer.valueOf(data[17]),data[18], data[19], Integer.valueOf(data[20]));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	 
		System.out.println("--- IMPORT FINISHED ---");
	  }
	
}
